<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Pp
 *
 * @author sunday add 2014.5.28
 */
require_once 'BaseChannel.php';

class Pp implements BaseChannel {
	function __construct() {
		//echo "construct";
	}

	//put your code here
	function android($postData) {
		$ret = $this->paymentCallBack($postData, ANDROID);
		echo $ret;
	}

	function ios($postData) {
		$ret = $this->paymentCallBack($postData, IOS);
		echo $ret;
	}

	//$notify_data其实就是$_post
	//$platform 平台 名字
	private function paymentCallBack($notify_data,$platform)
	{
		require_once('include/pp/Rsa.php');
		require_once('include/pp/MyRsa.php');
		
		//是否要把接收到的原始数据 urldecode处理 （php的$_POST 接收可以忽略这步）
		//get 方法传过来，需要urldecode下
		$notify_data = $_POST;
		$orderLog = new ChargeOrder();
		$orderLog->log("pp",$notify_data);		

		//$notify_data=$_POST;
		$chkres = self::chk($notify_data,$orderLog);
		
		//echo " chkres:$chkres \r\n";
		//$chkres = true;	//for test
		
		if(true == $chkres)
		{
			//验证通过
			//--------业务处理----------
			//0正常状态
			//1已兑换过并成功返回
			if (0 == $notify_data['status'])
			{
				$status = STATUS_SUCCESS; //1表示 支付成功
			} else {
				$status = STATUS_FAILD; //2表示 支付失败
			}

			if(true==$orderLog->recordOrder($status, $notify_data['amount'], $notify_data['billno'], "IosPP", $platform,$notify_data['order_id']))
			{
				$orderLog->log("pp","platfrom:".$platform." amount:".$notify_data['amount']." orderId:".$notify_data['billno']." success");
				return "success";
			}
			else
			{
				$orderLog->log("pp","platfrom:".$platform." amount:".$notify_data['amount']." orderId:".$notify_data['billno']." fail");
				//echo " orderId:".$notify_data['billno']." insert data to database error ";
				return "fail";
			}
		}else 
		{
			$orderLog->log("pp", " orderId:".$notify_data['billno']." chkres:$chkres check data error \r\n");
			return 'fail';
		}
	}

	//验证数据是否是合法数据
	private function chk($notify_data,$orderLog)
	{
		//print_r($notify_data);
		//echo "\r\n<br>";
		
		$privatedata = $notify_data['sign'];
		//echo "\r\n privatedata:$privatedata \r\n";
		//var_dump($privatedata);
		
		//一、使用 base64_decode 对上一步处理过的 sign 进行转换
		$privatebackdata = base64_decode($privatedata);
		//echo "\r\n privatebackdata:$privatebackdata \r\n";
		//var_dump($privatebackdata);
		
		//二、对接收到的数据 sign 使用开发者中心的公钥进行解码（rsa 解码）
		$MyRsa = new MyRsa;
		$data = $MyRsa->publickey_decodeing($privatebackdata, MyRsa::public_key);
		//echo "\r\n data:$data \r\n";
		//var_dump($data);
		
		$rs = json_decode($data,true);
		//echo "\r\n rs:$rs \r\n";
		//var_dump($rs);
		
		if(empty($rs)||empty($notify_data))
		{
			$orderLog->log("pp"," $rs or $notify_data is empty");
			$orderLog->log("pp", " json_decode or notify_data is empty\r\n");
			
			return false;
		}
		//三、解密后的数据与传过来的数据相同，则为成功
		if($rs["billno"] == $notify_data['billno']&&$rs["amount"] == $notify_data['amount']&&$rs["status"] == $notify_data['status'])
		{
			//echo " compare success \r\n";
			return true;
		}else
		{
			$orderLog->log("pp",array("billno"=>$rs["billno"],"billno2"=>$notify_data['billno'],
									'amount'=>$rs["amount"],'amount2' =>$notify_data['amount'],
									'status'=>$rs["status"] ,'status'=>$notify_data['status']));
			$orderLog->log("pp", " json_decode and notify_data did not match \r\n");
			
			return false;
		}
	}

}

?>
